package org.autocode.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.autocode.util.IOUtils;
import org.autocode.util.ObjectUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: autocode
 * @description:
 * @author: zhu.xiaojie
 * @create: 2020-08-16 09:41
 */
public class FileAutoConfig implements AutoConfig{

    public static final String DATASOURCES = "datasources";
    private String configDir;
    private String configFilePath;

    @Override
    public List<DatasourceConfig> getDatasourceConfig() {
        String text = getConfigText();
        if(ObjectUtils.isEmpty(text)){
            return new ArrayList<>();
        }

        JSONObject jsonObject = JSON.parseObject(text);
        List<DatasourceConfig> datasourceConfigs = jsonObject.getObject(DATASOURCES, new TypeReference<List<DatasourceConfig>>() {
        });

        return datasourceConfigs;
    }

    @Override
    public void addDatasource(DatasourceConfig config) {
        String text = getConfigText();
        if(ObjectUtils.isEmpty(text)){
            text = "{}";
        }
        JSONObject jsonObject = JSON.parseObject(text);
        List<DatasourceConfig> datasourceConfigs = jsonObject.getObject(DATASOURCES, new TypeReference<List<DatasourceConfig>>() {
        });
        if(datasourceConfigs == null){
            datasourceConfigs = new ArrayList<>();
        }
        datasourceConfigs.add(config);

        jsonObject.put(DATASOURCES , datasourceConfigs);

        this.save(jsonObject.toJSONString());
    }

    @Override
    public DatasourceConfig getDatasourceConfigByName(String name) {
        List<DatasourceConfig> datasourceConfig = getDatasourceConfig();
        DatasourceConfig config = datasourceConfig.stream().filter(c -> name.equals(c.getName())).findFirst().orElseGet(null);
        return config;
    }

    private void save(String text){
        try(FileOutputStream out = new FileOutputStream(this.configFilePath)){
            out.write(text.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private String getConfigText(){
        try(FileInputStream in = new FileInputStream(this.configFilePath)){
            String content = IOUtils.toString(in);
            return content;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    private FileAutoConfig() {
        String homePath = System.getProperty("user.home");
        String path = homePath + "/autocode";
        this.configDir = path;

        this.configFilePath = this.configDir + "/setting.json";

        File f1 = new File(this.configDir);
        if(!f1.exists()){
            f1.mkdirs();
        }
        File f2 = new File(this.configFilePath);
        if(!f2.exists()){
            try {
                f2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static final FileAutoConfig INSTANCE = new FileAutoConfig();

    public static FileAutoConfig getInstance(){
        return INSTANCE;
    }
}
